package com.example.mycarcalling.controller;

import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.stp.StpUtil;
import com.example.mycarcalling.bean.Navigation;
import com.example.mycarcalling.bean.Order;
import com.example.mycarcalling.service.NavigationService;
import com.example.mycarcalling.service.OrderService;
import com.example.mycarcalling.service.UserService;
import com.example.mycarcalling.utils.BaseResponse;
import com.example.mycarcalling.utils.ResponseResult;
import javafx.geometry.Pos;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
//@BaseResponse
@RequestMapping(value = "/order")
public class OrderController {

    @Resource
    private OrderService orderService;

    @Resource
    private NavigationService navigationService;

    @Resource
    private UserService userService;


    /**
     *  获取用户正在进行的订单
     * @param request
     * @return
     */
    @RequestMapping(method = RequestMethod.POST,value = "/getSendingOrder")
    public ResponseResult getSendingOrder(HttpServletRequest request){
        Long userId;
        Navigation navigation = null;
        Order order;
        try {
            userId = StpUtil.getLoginIdAsLong();
            System.out.println(userId);
            navigation = navigationService.getNotFinishNavigation(userId);
            if(null == navigation){
                throw new Exception("导航数据异常");
            }
            order = orderService.getSendingOrder(navigation.getNavigationId());
            if(null == order){
                throw new Exception("订单数据异常");
            }
            return new ResponseResult(200,"success",order);
        }catch (NotLoginException e){
            return new ResponseResult(503,e.getMessage(),null);
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseResult(500,e.getMessage(),null);
        }
    }


    /**
     * 获取司机已完成的订单
     * @param request
     * @return List类型的Order订单列表
     */
    @RequestMapping(method = RequestMethod.GET,value = "/getCompeteOrders")
    public ResponseResult getCompeteOrders(HttpServletRequest request){
        List<Order> orderList = null;

        try {
            if(userService.IsDriver(StpUtil.getLoginIdAsLong())) {
                orderList = orderService.getCompeteOrders(StpUtil.getLoginIdAsLong());
                if (null != orderList) {
                    return new ResponseResult(200, "success", orderList);
                }
                return new ResponseResult(201, "暂无已完成订单", null);
            }
            throw new Exception("没有司机权限");
        }catch (NotLoginException e){
            e.printStackTrace();
            return new ResponseResult(503,e.getMessage(),null);
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseResult(500,e.getMessage(),null);
        }
    }

    /**
     * 获取司机正在进行的订单
     * @param request
     * @return Order订单
     */
    @RequestMapping(method = RequestMethod.GET,value = "/getOnDrivingOrder")
    public ResponseResult getOnDrivingOrder(HttpServletRequest request){
        Order order = null;

        try {
            if(userService.IsDriver(StpUtil.getLoginIdAsLong())) {
                order = orderService.getOnDrivingOrder(StpUtil.getLoginIdAsLong());
                if (null != order) {
                    return new ResponseResult(200, "success", order);
                }
                return new ResponseResult(201, "暂无正在进行的订单", null);
            }
            throw new Exception("没有司机权限");
        }catch (NotLoginException e){
            e.printStackTrace();
            return new ResponseResult(503,e.getMessage(),null);
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseResult(500,e.getMessage(),null);
        }
    }

    /**
     * 司机通过导航信息id确认订单
     * @param navigationId 导航信息id
     * @param request
     * @return
     */
    @RequestMapping(method = RequestMethod.POST,value = "/orderConfirm/{navigationId}")
    public ResponseResult OrderConfirm(@PathVariable("navigationId") Long navigationId,HttpServletRequest request ){
        Order order = null;
        try {
            if(userService.IsDriver(StpUtil.getLoginIdAsLong())) {
                order = orderService.OrderConfirm(navigationId,StpUtil.getLoginIdAsLong());
                if (null != order) {
                    return new ResponseResult(200, "success", order);
                }
                return new ResponseResult(201, "订单可能被抢走了...", null);
            }
            throw new Exception("没有司机权限");
        }catch (NotLoginException e){
            e.printStackTrace();
            return new ResponseResult(503,e.getMessage(),null);
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseResult(500,e.getMessage(),null);
        }
    }

    /**
     * 乘客确认上车
     * @param orderId 订单id
     * @return
     */
    @RequestMapping(method = RequestMethod.POST,value = "/orderWithPickUp/{orderId}")
    public ResponseResult orderWithPickUp(@PathVariable("orderId") Long orderId){
        Long result = null;
        try {
            if(userService.IsDriver(StpUtil.getLoginIdAsLong())) {
                throw new Exception("没有乘客权限");
            }
            result = orderService.pickUpStatus(orderId);
            if (null != result) {
                return new ResponseResult(200, "success", result);
            }
            return new ResponseResult(201, "修改异常...", null);

        }catch (NotLoginException e){
            e.printStackTrace();
            return new ResponseResult(503,e.getMessage(),null);
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseResult(500,e.getMessage(),null);
        }
    }

    /**
     * 乘客确认下车
     * @param orderId 订单id
     * @return
     */
    @RequestMapping(method = RequestMethod.POST,value = "/orderWithGetOff/{orderId}")
    public ResponseResult orderWithGetOff(@PathVariable("orderId") Long orderId){
        Long result = null;
        try {
            if(userService.IsDriver(StpUtil.getLoginIdAsLong())) {
                throw new Exception("没有乘客权限");
            }
            result = orderService.getOffStatus(orderId);
            if (null != result) {
                return new ResponseResult(200, "success", result);
            }
            return new ResponseResult(201, "修改异常...", null);

        }catch (NotLoginException e){
            e.printStackTrace();
            return new ResponseResult(503,e.getMessage(),null);
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseResult(500,e.getMessage(),null);
        }
    }


    /**
     * 获取司机正在进行的订单
     * @param request
     * @return Order订单
     */
    @RequestMapping(method = RequestMethod.POST,value = "/getReadyCompeteOrder/{orderId}")
    public ResponseResult getReadyCompeteOrder(@PathVariable("orderId") Long orderId,HttpServletRequest request){
        Order order = null;

        try {
            if(userService.IsDriver(StpUtil.getLoginIdAsLong())) {
                order = orderService.getReadyCompeteOrder(orderId);
                if (null != order) {
                    return new ResponseResult(200, "success", order);
                }
                return new ResponseResult(201, "暂无所查询的订单", null);
            }
            throw new Exception("没有司机权限");
        }catch (NotLoginException e){
            e.printStackTrace();
            return new ResponseResult(503,e.getMessage(),null);
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseResult(500,e.getMessage(),null);
        }
    }


}
